package com.naiterui.ehp.bs.doctor.dao.impl;

import com.naiterui.common.repository.page.Page;
import com.naiterui.common.repository.page.PageParamVO;
import com.naiterui.ehp.bp.domain.PatientRecord;
import com.naiterui.ehp.bs.doctor.dao.IPatientRecordDao;
import com.naiterui.ehp.bs.doctor.repository.IPatientRecordRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Description 患者病历持久层

 * @version 2015年6月22日 下午1:39:23 by chenlin
 */
@Repository
public class AdPatientRecordDaoImpl implements IPatientRecordDao {

    @Autowired
    private IPatientRecordRepository patientRecordRepository;

    @Override
    public Page<PatientRecord> findPageRecordsByType(Long doctorId, Long patientId, Integer type, int page, int num) {
        Pageable pageable = new PageParamVO(page, num).getPageable( Sort.by("createdAt").descending());
        org.springframework.data.domain.Page<PatientRecord> result = patientRecordRepository.findByDoctorIdAndPatientIdAndType(doctorId,patientId, type, pageable);
        return Page.buildPage(page, num, result.getContent(), result.getTotalElements());
    }

    @Override
    public List<PatientRecord> findContactRecords(Long contactId, Long doctorId) {
        List<PatientRecord> records  = patientRecordRepository.findByPatientIdAndDoctorIdAndType(contactId, doctorId, PatientRecord.RECORD_TYPE_CONTACTS);
        return records;
    }

    @Override
    public PatientRecord findPatientRecordByUuid(String uuid) {
        List<PatientRecord> list = patientRecordRepository.findByUuid(uuid);
        if (list == null) {
            return null;
        } else {
            return list.isEmpty() ? null : list.get(0);
        }
    }

}
